﻿using ITXHWEB.Models;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Web.Mvc;
using WPFExcelReport;
using WPFExcelReport.Helpers;
using ITXHWEB.Infrastructure;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace ITXHWEB.Controllers
{
    public partial class WBBSTController : ITXController
    {
        [ChildActionOnly]
        public ActionResult MPBWB06(string returnUrl)
        {
            ModelState.Clear();
            MWB.IsWHReady = true;
            MWB.INFO = "";
            MWB.INFO = INFO;
            ViewBag.ReturnUrl = returnUrl; 
            foreach (KeyValuePair<string,string>kp in MWB.WHPCS)
            {
                 MWB.INFO += kp.Key+":" + kp.Value + ";";
            }
            return PartialView(MWB);
        }
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult MPBWB06(WBBSTModel model, string returnUrl)
        {

            try
            {
                MWB.WHPCS.Clear();
                MWB.INFO = "";
                CHKSTK();
            }
            catch (Exception ex)
            {
                //VKPAUF = ex.Message;
                Tracer.Error(ex.StackTrace, ex);
            }
            return RedirectToAction(MAPP, MAPP);
        }
        [ChildActionOnly]
        public ActionResult PBWB06(string returnUrl)
        {
            ModelState.Clear();
            ViewBag.ReturnUrl = returnUrl;
            MWB.SLO = "01";
            MWB.SLB = "ZZ";
            MWB.LO = "01";
            MWB.LB = "PB";
            MWB.LP = "01      ";
            MWB.WHP = "01PB01      ";
            if (!MWB.IsWHReady)
            {
                INFO = "请扫描目的仓库";
            }
            return PartialView(MWB);
        }
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult PBWB06(WBBSTModel model, string returnUrl)
        {
            try
            {
                MOVE2PB(model);//移库操做  
               
            }
            catch (Exception ex)
            {
                //ModelState.AddModelError("",ex.Message);
                Tracer.Error(ex.StackTrace, ex);
            }
           
           
            return RedirectToAction(MAPP, MAPP);
        }
        
        private void CHKSTK()
        {
            dwData = VHP.WBB.TBMain.NewRow();
            dwData["WBBMANDANT"] = app.Mandant;
            dwData["WBBWAKZ"] = MWB.WAKZ;
            dwData["WBBART"] = MWB.ART;
            dwData["WBBVAR"] = MWB.VAR;
            dwData["WBBFARBE"] = MWB.FARBE;
            DataSet SResult = VHP.WBB.QWB25(dwData);
            MWB.INFO = "";
            MWB.WHPCS.Clear();
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                MWB.WHPCS.Add(row["WBBLB"].ToString(),  row["PCS"].ToString());
                MWB.INFO += row["WBBLB"].ToString() + row["PCS"].ToString() + ";";
            }
        }
        public void MOVE2PB(WBBSTModel model)
        {
            if (model == null)
                return;
            if (model.WBBSTKNR == null)
                return;
            model.WBBSTKNR = model.WBBSTKNR.Trim().PadRight(12);
            dwData = VHP.WBB.TBMain.NewRow();
            dwData["WBBMANDANT"] = app.Mandant;
            dwData["WBBSTKNR"] = model.WBBSTKNR.Trim().PadRight(12);
            dwData["WBBLO"] = MWB.SLO;
            dwData["WBBLB"] = MWB.SLB;
            DataSet SResult = VHP.WBB.QWB22(dwData);
            MWB.REMARK = "";
            if (SResult.Tables[0].Rows.Count == 0)
            {
                MWB.IsDataValid = false;
                INFO = "不在范围内";
                return;
            }
            MWB.IsDataValid = true;
            DataRow _dwWB = SResult.Tables[0].Copy().Rows[0];
            VHP.WBB.MOVE2PB(_dwWB, MWB.LO, MWB.LB, MWB.LP, RUNID);
            INFO = "完成(" + model.WBBSTKNR.Trim() + ")";
            if (MWB.WAKZ + MWB.ART + MWB.VAR + MWB.FARBE != _dwWB["ART"].ToString())
            {
                MWB.WAKZ = _dwWB["WBBWAKZ"].ToString();
                MWB.ART = _dwWB["WBBART"].ToString();
                MWB.VAR = _dwWB["WBBVAR"].ToString();
                MWB.FARBE = _dwWB["WBBFARBE"].ToString();
                MWB.WHPCS.Clear();
                CHKSTK();
            }
            else
            {
                int pcs = 0;
                if (MWB.WHPCS.ContainsKey(MWB.SLB))
                {
                    pcs = Convert.ToInt32(MWB.WHPCS[MWB.SLB]);
                    MWB.WHPCS[MWB.SLB]=(pcs--).ToString();
                }
                else
                {
                    MWB.WHPCS[MWB.LB] = "-1";
                }
                if (MWB.WHPCS.ContainsKey(MWB.LB))
                {
                    pcs = Convert.ToInt32(MWB.WHPCS[MWB.LB]);
                    MWB.WHPCS[MWB.SLB] = (pcs++).ToString();
                }
                else
                {
                    MWB.WHPCS[MWB.LB] = "1";
                }
            }
        }
    }
}
